package day_15.homework;


import java.util.Random;

/**
 * @author ck
 * @date 2021/8/24 14:58
 */

public class MyThread1 implements Runnable {
    private final String s = "11";
    int a = 0;
    int b = 0;

    @Override
    public void run() {
        Random r = new Random();
        while (true) {
            int number = r.nextInt(12) + 1;
            //悲观锁
            synchronized (s) {
                if (!GiftPool.list.isEmpty()) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (GiftPool.list.containsKey(number)) {
                        System.out.println(Thread.currentThread().getName() + "又产生了一个" + GiftPool.list.get(number) + "元大奖");
                        GiftPool.list.remove(number);
                        if ("车轲".equals(Thread.currentThread().getName())) {
                            a++;
                        }
                        if ("方浩".equals(Thread.currentThread().getName())) {
                            b++;
                        }
                        if (GiftPool.list.isEmpty()) {
                            System.out.println("车轲共抽取了" + a + "次");
                            System.out.println("方浩共抽取了" + b + "次");
                        }
                    }
                } else {
                    break;
                }
            }
        }
    }

}
